import time
from selenium import webdriver
from selenium.webdriver.common.by import By

class MailOperation:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://mail.163.com/')
        self.driver.maximize_window()

    def login(self):
        # 找到登录页frame并切换
        login = self.driver.find_element(By.TAG_NAME, 'iframe')
        self.driver.switch_to.frame(login)
        # 找到账号输入框清空并输入
        time.sleep(2)
        self.driver.find_element(By.NAME, 'email').clear()
        self.driver.find_element(By.NAME, 'email').send_keys('ly134****3230')
        # 找到密码输入框清空并输入
        time.sleep(2)
        self.driver.find_element(By.NAME, 'password').clear()
        self.driver.find_element(By.NAME, 'password').send_keys('Ly********')
        # 找到登录按钮并点击
        time.sleep(2)
        self.driver.find_element(By.ID, 'dologin').click()
        time.sleep(2)

    def post(self):
        # 找到写信按钮并点击
        self.driver.find_element(By.XPATH,'//*[@id="_mail_component_149_149"]/span[2]').click()
        # 隐式等待
        self.driver.implicitly_wait(10)
        time.sleep(2)
        # 找到收件人并输入收件人邮箱
        self.driver.find_element(By.CLASS_NAME,'nui-editableAddr-ipt').send_keys('jh135****3364@163.com')
        # 找到主题输入框并输入主题
        self.driver.find_element(By.XPATH,'//input[contains(@id,"_subjectInput")]').send_keys('干饭')
        time.sleep(2)
        # 找到iframe切换
        iframe = self.driver.find_element(By.CLASS_NAME,'APP-editor-iframe')
        self.driver.switch_to.frame(iframe)
        # 找到内容输入框并输入内容
        self.driver.find_element(By.CLASS_NAME,'nui-scroll').send_keys('小姐姐，明天要不要约饭')
        # 切换回到主页frame
        time.sleep(2)
        self.driver.switch_to.default_content()
        # 找到发送按钮并点击
        self.driver.find_element(By.XPATH,'//div[@class="nui-toolbar-item"]/div/span[2]').click()

    def quit(self):
        time.sleep(2)
        self.driver.quit()


if __name__ == '__main__':
    mo = MailOperation()
    mo.login()
    mo.post()
    mo.quit()

